package com.hup.minicontroller.client.model;

import android.bluetooth.BluetoothDevice;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.hup.minicontroller.client.service.LanClientService;
import com.hup.minicontroller.client.view.ButtonFloaterView;
import com.hup.minicontroller.common.bluetooth.BtConstant;
import com.hup.minicontroller.common.constant.BroadcastConstant;
import com.hup.minicontroller.common.constant.NetConstant;

import java.util.UUID;

import lombok.Getter;
import lombok.Setter;

@Getter
public class AppConfig {
    public static final int NULL_BUTTON_FLOATER_LOCATION = -1;

    private SharedPreferences sp;

    /**
     * 局域网server端口号,可以按需动态修改
     */
    private int lanServerPort = NetConstant.LAN_SERVER_PORT;
    /**
     * 局域网配置
     * 用户在界面上输入的ip,用于连接服务端 {@link LanClientService#switchClient(boolean)}
     */
    private String lanInputIp = "192.168.1.101";
    private int lanBroadcastPort = BroadcastConstant.PORT;
    private String broadcastIp = BroadcastConstant.IP;

    //蓝牙配置
    private UUID btUuid = BtConstant.BT_UUID;
    /**
     * 要连接的蓝牙服务端
     */
    @Setter
    private BluetoothDevice bluetoothServer;

    /**
     * 平铺投屏图像
     */
    private boolean drawFull = false;

    /**
     * 悬浮窗中显示功能按钮
     */
    private boolean showFunctionButton = false;
    public float buttonFloaterX, buttonFloaterY;

    public void loadConfig(SharedPreferences spInput) {
        sp = spInput;
        lanInputIp = sp.getString("lanInputIp", lanInputIp);
        drawFull = sp.getBoolean("drawFull", drawFull);
        showFunctionButton = sp.getBoolean("showFunctionButton", showFunctionButton);
        buttonFloaterX = sp.getFloat("buttonFloaterX", NULL_BUTTON_FLOATER_LOCATION);
        buttonFloaterY = sp.getFloat("buttonFloaterY", NULL_BUTTON_FLOATER_LOCATION);
    }

    public void setLanInputIp(String lanInputIp) {
        this.lanInputIp = lanInputIp;
        sp.edit().putString("lanInputIp", lanInputIp).apply();
    }

    public void setDrawFull(boolean drawFull) {
        this.drawFull = drawFull;
        sp.edit().putBoolean("drawFull", drawFull).apply();
    }

    public void setShowFunctionButton(boolean showFunctionButton) {
        this.showFunctionButton = showFunctionButton;
        sp.edit().putBoolean("showFunctionButton", showFunctionButton).apply();
    }

    /**
     * 保存 {@link ButtonFloaterView} 的坐标,坐标都是百分比,能适应横竖屏
     */
    public void setButtonFloaterLocation(float xPercent, float yPercent) {
        this.buttonFloaterX = xPercent;
        this.buttonFloaterY = yPercent;
        Editor edit = sp.edit();
        edit.putFloat("buttonFloaterX", buttonFloaterX);
        edit.putFloat("buttonFloaterY", buttonFloaterY);
        edit.apply();
    }

}
